
import java.sql.*;
import javax.swing.JOptionPane;



public class Request {
    final static int TYPE_MEDICAL = 0;
    final static int TYPE_CHANGE = 1;
    final static int TYPE_OFFCAMPUS = 2;
    final static int TYPE_HOUSEASSIGNMENT = 3;
    
    final static int STATUS_NEW = 0;
    final static int STATUS_REVIEWING = 1;
    final static int STATUS_APPROVED = 2;
    final static int STATUS_DISAPPROVED = 3;
    
    
    public int requestID;
    public int studentID;
    public int status;
    public int type;
    public int year;
    public String reason;
    public String idealRoom;

    private Connection connect;
    
    public Request(int requestID, Connection connect) {
        this.requestID = requestID;
        this.connect = connect;
        loadFromDB();
    }
    
    
    public boolean update(){
        try {
            String updateQuery = "UPDATE DORMS.request SET status = ?,"
                    + "type = ?, year = ?, reason = ?, idealRoom = ? "
                    + "WHERE requestID = ?";
            PreparedStatement updateStmt = connect.prepareStatement(updateQuery);
            updateStmt.setInt(1, status);
            updateStmt.setInt(2, type);
            updateStmt.setInt(3, year);
            updateStmt.setString(4, reason);
            updateStmt.setString(5, idealRoom);
            updateStmt.setInt(6, requestID);
            updateStmt.execute();
            return true;
        }catch (Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Unable to update to database");
             
        }
        return false;
    }
    
    public String toString(){
            String title = "";
            
        try{
            title = getTypeAsString() + ": ";
            
            
            String getStudentQuery = "SELECT firstName,lastName FROM student "
                    + "where studentID = ?";
            PreparedStatement getStudentStmt = connect.prepareStatement(getStudentQuery);
            getStudentStmt.setInt(1, studentID);
            ResultSet studentResult = getStudentStmt.executeQuery();
            if(studentResult.next())
                title += studentResult.getString("firstName") +" " + studentResult.getString("lastName");
            
            
            title += " - " + getStatusAsString();
            
                
            
        } catch(Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Unable to get room assignment.");
            
        }
        
        return title;
        
    }
    
    public String getTypeAsString(){
        if (type == TYPE_MEDICAL)
                return "MEDICAL";
        else if (type == TYPE_CHANGE)
                return "CHANGE";
        else if (type == TYPE_OFFCAMPUS)
                return"OFFCAMPUS";
        else if (type == TYPE_HOUSEASSIGNMENT)
                return "HOUSEASSIGNMENT";
        return "";
            
    }
    
    public String getStatusAsString(){
            if (status == STATUS_NEW)
                return "NEW";
            else if (status == STATUS_REVIEWING)
                return "REVIEWING";
            
            else if (status == STATUS_APPROVED)
                return "APPROVED";
            
            else if (status == STATUS_DISAPPROVED)
                return "DISAPPROVED";
            
            return "";
            
    }
    
    public boolean mark(int status){
        this.status = status;
        if(update())
            return true;
        return false;
    }
    
    
    //private methods start here
    
    private void loadFromDB(){
        try {
            String loadQuery = "SELECT * FROM DORMS.request WHERE requestID = ?";
            PreparedStatement loadStmt = connect.prepareStatement(loadQuery);
            loadStmt.setInt(1, requestID);
            ResultSet requestInfo = loadStmt.executeQuery();
            if(requestInfo.next()){
                studentID = requestInfo.getInt("studentID");
                status = requestInfo.getInt("status");
                type = requestInfo.getInt("type");
                year = requestInfo.getInt("year");
                reason = requestInfo.getString("reason");
                idealRoom = requestInfo.getString("idealRoom");
            }
            
        } catch (Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Unable to load request from database.");
            
        }
        
    }
    
}
